package com.summer.data.apt;

import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;

import javax.lang.model.element.Modifier;
import java.util.Set;

/**
 * @author ydfqy
 */
public class ClassUtil {

    public static boolean innerClassExists(String clazzName, JCTree.JCClassDecl classDecl) {
        for (JCTree def : classDecl.defs) {
            if (def instanceof JCTree.JCClassDecl) {
                if (((JCTree.JCClassDecl) def).name.contentEquals(clazzName)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean methodExists(String methodName, JCTree.JCClassDecl classDecl) {
        for (JCTree def : classDecl.defs) {
            if (def instanceof JCTree.JCMethodDecl) {
                if (((JCTree.JCMethodDecl) def).name.contentEquals(methodName)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static JCTree.JCMethodDecl getMethodDecl(String methodName, JCTree.JCClassDecl classDecl) {
        for (JCTree def : classDecl.defs) {
            if (def instanceof JCTree.JCMethodDecl) {
                JCTree.JCMethodDecl methodDecl = (JCTree.JCMethodDecl) def;
                if (methodDecl.name.contentEquals(methodName)) {
                    return methodDecl;
                }
            }
        }
        return null;
    }

    public static String toGetterName(JCTree.JCVariableDecl field) {
        boolean isBoolean = field.vartype.toString().equals("boolean");
        String prefix = isBoolean ? "is" : "get";
        return prefix + toTitleCase(field.getName().toString());
    }

    public static String toSetterName(JCTree.JCVariableDecl field) {
        return "set" + toTitleCase(field.getName().toString());
    }

    public static String toTitleCase(String str) {
        char first = str.charAt(0);
        if (first >= 'a' && first <= 'z') {
            first -= 32;
        }
        return first + str.substring(1);
    }

    public static String getShareTableName(JCTree.JCClassDecl classDecl) {
        String shareTableName = classDecl.name.toString();
        if (shareTableName.length()>0) {
            shareTableName = shareTableName.substring(0, 1).toLowerCase() + shareTableName.substring(1);
        }
        return shareTableName;
    }
}
